﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace Orion.Models
{
    public class UserRepository
    {
        OrionDBDataContext m_db = new OrionDBDataContext();

		public IEnumerable<User> GetAllUsers(int id = 0 )
		{
			var result = ( from u in m_db.Users
			              orderby u.Valid , u.FirstName , u.MiddleName , u.LastName
			              select u ).Skip( id * 20 ).Take( ( id + 1 ) * 20) ;

			return result;
		}

		public User GetUserById(int id)
		{
			var result = (from u in m_db.Users
						  where u.ID == id
						  select u).SingleOrDefault();

			return result;
		}

		public void UpdateUser(User u)
		{
			User temp = (from t in m_db.Users
						 where t.ID == u.ID
						 select t).SingleOrDefault();
			
			//incase pic is not changed
			if( ! string.IsNullOrEmpty( u.Picture ) )
			{
				temp.Picture = u.Picture;
			}
			temp.Address = u.Address;
			temp.FirstName = u.FirstName;
			temp.HomePhone = u.HomePhone;
			temp.LastName = u.LastName;
			temp.MiddleName = u.MiddleName;
			temp.MobilePhone = u.MobilePhone;
			temp.PostCode = u.PostCode;
			temp.SSN = u.SSN;
			temp.Valid = u.Valid;
            temp.Info = u.Info;
			temp.WorkPhone = u.WorkPhone;

			m_db.SubmitChanges();
		}

		public void AddUser( User u )
		{
			m_db.Users.InsertOnSubmit( u );
			m_db.SubmitChanges( );
		}
    }
}